package com.test.qnews.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.android.volley.toolbox.NetworkImageView;
import com.test.qnews.R;
import com.test.qnews.beans.VideoBeans;
import com.test.qnews.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/19.
 */
public class VideoAdapter extends BaseAdapter {
    private List<VideoBeans> data;
    private Context context;

    public VideoAdapter(List<VideoBeans> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Viewholder holder = new Viewholder();
        if(convertView == null){  //判断行布局为空
            //加载行布局文件
            convertView = LayoutInflater.from(context).inflate(R.layout.item_video,null);
            //绑定行布局ID
            holder.iv = (NetworkImageView) convertView.findViewById(R.id.image_item_video_iv);
            holder.title_tv = (TextView) convertView.findViewById(R.id.title_item_video_tv);
            holder.videoView = (VideoView) convertView.findViewById(R.id.item_video);
            //将缓存类对象设置到行布局当中
            convertView.setTag(holder);
        }else{
            //直接使用行布局
            holder = (Viewholder) convertView.getTag();
        }

        VolleySingleton.getInstance().loadImage(holder.iv, data.get(position).getCover());
        holder.title_tv.setText(data.get(position).getTitle());
        Uri uri = Uri.parse(data.get(position).getVideoUrl());
        holder.videoView.setMediaController(new MediaController(context));
        holder.videoView.setVideoURI(uri);
        holder.videoView.requestFocus();
//
//        holder.iv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                switch (v.getId()) {
//                    case R.id.image_item_video_iv:
//                    holder.videoView.start();
//                    break;
//                }
//            }
//        });
        return convertView;
    }



    //创建缓存类
    private class Viewholder{
        private NetworkImageView iv;
        private TextView title_tv;
        private VideoView videoView;
    }
}
